package dyu.Questionnaire;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class InputPhoneNumber extends Activity {
	SharedPreferences prefs;
	String prefName = MainActivity.prefName;
	
	
	@Override
	 
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		 setContentView(R.layout.inputphonenumber);
		 final EditText edittext = (EditText)findViewById(R.id.txtPhoneNumber);
		Button btnOk = (Button) findViewById(R.id.btnOK);
		btnOk.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				//prefs = getApplication().getSharedPreferences(prefName, MODE_PRIVATE);
				prefs = getSharedPreferences(prefName, MODE_PRIVATE);
		    	SharedPreferences.Editor editor = prefs.edit();
		        editor.putBoolean("firstRun", false);
		        editor.commit();
		        boolean firstRun = prefs.getBoolean("firstRun", true);
		        if(firstRun == false)
		        {
		        	if(edittext.getText().toString().length()<7)
		        	{
		        		Toast.makeText(getBaseContext(), "Please input correct phone number", Toast.LENGTH_LONG).show();
		        		edittext.setSelection(edittext.getText().length());
		        	
		        	}
		        	else
		        	{
		        		editor.putString("phoneNumber", edittext.getText().toString());
			        	editor.commit();
			        	String phoneNumber = prefs.getString("phoneNumber", "0");
			        	Toast.makeText(getBaseContext(),"Received Phone Number is " + phoneNumber, Toast.LENGTH_LONG).show();
			        	Intent i = new Intent(InputPhoneNumber.this, MainActivity.class);
						startActivity(i);
						
						finish();
		        	}
		        }
				
			}
		});
		
		Button btnCancel = (Button) findViewById(R.id.btnCancel);
		btnCancel.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				edittext.setText("+886");
				edittext.setSelection(edittext.getText().length());
			}
		});
	}
	//---Disable Home button ---///
		public void onAttachedToWindow() {
		    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
		    super.onAttachedToWindow();
		    
		}
	

}
